<?php
require_once('BaseStatsReportForm.class.php');

class EmployeeLeavesPercentage extends BaseStatsReportForm
{

    public function getOrderByFields(){
        return array (
        EmployeePeer::CODE  => 'employee code'
        , EmployeeI18nPeer::FIRST_NAME  => 'name'
        , EmployeePeer::CARD_ID  => 'card id'
        , EmployeePeer::HIRING_DATE  => 'hiring_date'
        );
    }


    public $display_fields = array(
    'serial'        => 'Serial'
    , 'employee_code' => 'Employee Code'
    , 'employee_name' => 'Employee name'
    , 'month_year'    => 'Month/Year'
    );

    public  function getDisplayFields()
    {
        $fields = array();
        foreach (CodeLeaveTypePeer::getAll(sfConfig::get('list_leave_type_id',array())) as $leave_type){
            $fields['leave_type_percentage_'.$leave_type->getId()] = $leave_type->__toString();
        }
        if(sfConfig::get('add_total_leave_period',false)){
            $fields = array_merge($fields,array('total_period'  => 'Total period'));
        }
        return array_merge( $this->display_fields , $fields );
    }



    public function configure(){
        parent::configure();

        unset($this['order_by']);

        $c = new Criteria();
        $c->add(CodeLeaveTypePeer::OUT_OF_CALC , 0 );

        $this->widgetSchema['list_leave_type_id'] = new sfWidgetFormPropelChoice(array('multiple'=>true,'model'=>'CodeLeaveType','criteria'=>$c));
        $this->validatorSchema['list_leave_type_id'] = new sfValidatorPropelChoice(array('multiple'=>true,'model'=>'CodeLeaveType','criteria'=>$c));

        $this->widgetSchema['leave_percentage'] = new sfWidgetLogicalPeriod();
        $this->validatorSchema['leave_percentage'] = new sfvalidatorLogicalPeriod();

        $this->widgetSchema['month_year'] = new sfWidgetFormDate(array('format'=>'<span style="display:none">%day%</span>%month%/%year%','can_be_empty'=>false));
        $this->validatorSchema['month_year'] = new sfValidatorDate();

        $this->widgetSchema['show_totals'] = new sfWidgetFormInputCheckbox();
        $this->validatorSchema['show_totals'] = new sfValidatorBoolean();




        $this->getWidgetSchema()->moveField('section_id','before','employee_id');
        $this->getWidgetSchema()->moveField('department_id','before','section_id');
        $this->getWidgetSchema()->setDefault('group_by','all');



        unset($this['leave_type_id'],$this['leave_start'],$this['leave_end']);

    }


    protected function addListLeaveTypeIdColumnCriteria(Criteria $criteria, $field, $values){
        if( is_array($values) && count($values) > 0 ) {
           
/*            $criteria->add( LinkEmployeeLeavePeer::LEAVE_TYPE_ID , $values , Criteria::IN );
            
            $criteria->addAscendingOrderByColumn(LinkEmployeeLeavePeer::LEAVE_TYPE_ID );
*/
            $criteria->addGroupByColumn(LinkEmployeeLeavePeer::EMPLOYEE_ID );
            sfConfig::set('list_leave_type_id',$values);
        }
    }

    protected function addShowTotalsColumnCriteria(Criteria $criteria, $field, $value){
        if(!empty($value)){
            sfConfig::set('add_total_leave_period',true);
        }
    }



    protected function addLeavePercentageColumnCriteria(Criteria $criteria, $field, $values){
        //var_dump($value);exit;
        if(is_array($values) && !empty($values['logical']) && !empty($values['input'])  ){
            $values['input'] = $values['input'] / 100;
            if($group_by = sfConfig::get('group_by')){
                if(stripos($group_by,'department')!==false){
                    $criteria->add(LinkEmployeeLeavePeer::DEPT_PERCENTAGE  , $values['input'] , $values['logical']);
                }
                if(stripos($group_by,'section')!==false){
                    $criteria->add(LinkEmployeeLeavePeer::SECTION_PERCENTAGE , $values['input'] , $values['logical']);
                }
            }else{
                $criteria->add(LinkEmployeeLeavePeer::LEAVE_PERCENTAGE ,$values['input'] , $values['logical']);
            }
        }

    }


    protected function addMonthYearColumnCriteria(Criteria $criteria, $field, $value){
        
        if(!empty($value)){
            $criteria->add(LinkEmployeeLeavePeer::MONTH ,date('m',strtotime($value)));
            $criteria->add(LinkEmployeeLeavePeer::YEAR  ,date('Y',strtotime($value)));
            sfConfig::set('add_total_leave_period',true);
        }
    }

    

}
